package com.block.datapush.form.item;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.enums.BaseTypeInfoEnum;
import com.block.common.service.LogFactory;
import com.block.datapush.form.service.IFormPushHandle;
import com.block.datapush.model.dto.FormPushResultDTO;
import com.block.datapush.form.item.util.HuiZhongUtil;
import com.block.db.entity.FormInfo;
import com.block.db.entity.FormInfoHis;
import com.block.db.entity.MemberExtInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class HuiZhongPushHandleService implements IFormPushHandle {

    private static final Logger log = LogFactory.getCommLog();

    @Override
    public boolean queryMatchResult(FormInfo formInfo, MemberExtInfo memberExtInfo) {

        try {
            String queryUrl = formInfo.getQueryUrl();
            String mobileMd5 = SecureUtil.md5(memberExtInfo.getMobile()).toLowerCase();

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("phoneNo", mobileMd5);
            paramMap.put("channelSource", formInfo.getApiKey());

            log.info("【惠众推送】撞库开始【queryUrl】{}【bizParam】{}【paramMap】{}", queryUrl, JSON.toJSONString(paramMap), JSON.toJSONString(paramMap));
            String result = HttpUtil.createPost(queryUrl)
                    .header("Content-Type", "application/json")
                    .body(JSON.toJSONString(paramMap))
                    .execute().body();
            log.info("【惠众推送】撞库结束【result】{}", result);

            JSONObject resultObject = JSON.parseObject(result);
            String code = resultObject.getString("code");
            String msg = resultObject.getString("msg");

            if (StrUtil.equals(code, "0000")){
                return true;
            }
            return false;
        }catch (Exception exception){
            exception.printStackTrace();
        }
        return false;
    }

    @Override
    public FormPushResultDTO pushMemberInfo(FormInfo formInfo, MemberExtInfo memberExtInfo, String orderNo, JSONObject extObject) {

        FormPushResultDTO pushResultDTO = new FormPushResultDTO();
        pushResultDTO.setOrderNo(orderNo);
        try {

            String itemId = extObject.getString("itemId");

            String requestUrl = formInfo.getRequestUrl();
            Map<String, Object> bizParamMap = new HashMap<>();

            bizParamMap.put("cust_name", memberExtInfo.getUsername());
            bizParamMap.put("mobile", memberExtInfo.getMobile());
            bizParamMap.put("age", "0");
            bizParamMap.put("sex", "0");
            bizParamMap.put("city", memberExtInfo.getCity()); //
            bizParamMap.put("apply_limit", BaseTypeInfoEnum.LoanAmount.getEntryByValue(memberExtInfo.getLoanAmount()).getAmount());
            bizParamMap.put("channel_source", formInfo.getApiKey());

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("data", HuiZhongUtil.encrypt(JSON.toJSONString(bizParamMap), HuiZhongUtil.key));

            log.info("【惠众推送】导单开始【requestUrl】{}【bizParam】{}【paramMap】{}", requestUrl, JSON.toJSONString(bizParamMap), JSON.toJSONString(paramMap));
            String result = HttpUtil.createPost(requestUrl)
                    .header("Content-Type", "application/json")
                    .body(JSON.toJSONString(paramMap))
                    .execute().body();
//            String result = "{\"data\":{\"error_info\":\"申请已提交\",\"error_no\":0}}";
            log.info("【惠众推送】导单结束【result】{}", result);

            JSONObject resultObject = JSON.parseObject(result);
            JSONObject dataObject = resultObject.getJSONObject("data");
            Integer errorNo = dataObject.getInteger("error_no");
            String errorInfo = dataObject.getString("error_info");

            pushResultDTO.setResult(result);
            pushResultDTO.setCode(String.valueOf(errorNo));
            pushResultDTO.setMessage(errorInfo);
            pushResultDTO.setParamContent(JSON.toJSONString(bizParamMap));
            if (ObjectUtil.equal(errorNo, Integer.valueOf(0))){
                pushResultDTO.setSuccess(true);
            }else{
                pushResultDTO.setSuccess(false);
            }
            return pushResultDTO;
        }catch (Exception exception){
            exception.printStackTrace();
        }
        pushResultDTO.setSuccess(false);
        return pushResultDTO;
    }

    @Override
    public void afterPushHandle(FormInfoHis formInfoHis) {

    }
}
